let express = require("express");
let path = require("path");
let fs = require("fs");
const router = express.Router();
const connect = require("../config/config.js");
const log4js  = require("../config/log.js");
let logger=log4js.getLogger("info");


//玩家信息查询
router.post("/playerinfo", (req, res) => {
	let uid = req.body.uid;
	let nickname = req.body.nickname;
	connect((err, db) => {
		if (err) {
			logger.info("数据库链接失败！")
		} else {
			if (nickname === "") {
				db.collection("userinfo").find({
					uid: parseInt(uid)
				}).toArray(function(err, result) {
					if (err) {
						return;
					} else {
						res.send({
							code: 200,
							result: result,
							msg: '请求数据成功'
						})
					}
				})
			} else if (uid === "") {
				db.collection("userinfo").find({
					nickname: nickname
				}).toArray(function(err, result) {
					if (err) {
						return;
					} else {
						res.send({
							code: 200,
							result: result,
							msg: '请求数据成功'
						})
					}
				})
			} else {
				return;
			}
		}
	})
});

//新手引导
router.post('/noviceguide', (req, res) => {
	const first_time = parseInt(req.body.first_time);
	const last_time = parseInt(req.body.last_time);
	const server_id = parseInt(req.body.server_id);
	const map = new Map();
	let guides_arr = [];let new_guide_arr = [];let arr = [];
	const file = path.join(__dirname, '../map/guides.json');
	//读取json文件
	fs.readFile(file, 'utf8', function(err, data){
		if (err) {
			res.send('文件读取失败');
		} else {
			let guides = JSON.parse(data);
			guides.forEach(item => {
				guides_arr.push(item.event_id);
				map.set(item.event_id, item.description)
			});
			connect((err, db) => {
				if (err) {
					logger.info("数据库链接失败！")
				} else {
					db.collection("new_guide").aggregate([{
							$match: {
								$and: [{
										server_id: server_id
									},
									{
										date: {
											"$gte": first_time,
											"$lte": last_time
										}
									}
								],
							}
						},
						{
							$group: {
								_id: "$event_id",
								count: {
									$sum: 1
								}
							}
						},
					]).toArray(function(err, result) {
						if (err) {
							logger.info('error:' + err);
							return;
						} else {
							result.forEach(item => {
								new_guide_arr.push({
									event_id: item._id,
									count: item.count
								});
							});
							new_guide_arr.sort(function(a, b) {
								return guides_arr.indexOf(a.event_id.toString()) - guides_arr.indexOf(b.event_id.toString());
							});
							new_guide_arr.forEach(item => {
								if (map.get(item.event_id.toString()) === undefined) {
									return;
								} else {
									let obj = {
										event_id: map.get(item.event_id.toString()),
										count: item.count
									};
									arr.push(obj)
								}
							});
							res.send({
								code: 200,
								msg: "success",
								result: arr
							})	
						}
					})
				}
			})
		}
	});
});

//充值玩家分布
router.post('/playrecharge', (req, res) => {
	let server_id = req.body.server_id;
	let number_of_people1 = 0;let number_of_people2 = 0;
	let number_of_people3 = 0;let number_of_people4 = 0;
	let number_of_people5 = 0;let number_of_people6 = 0;
	let number_of_people7 = 0;let number_of_people8 = 0;
	let number_of_people9 = 0;let number_of_people10 = 0;
	let sum_of_money1 = 0;let sum_of_money2 = 0;
	let sum_of_money5 = 0;let sum_of_money6 = 0;
	let sum_of_money7 = 0;let sum_of_money3 = 0;
	let sum_of_money4 = 0;let sum_of_money8 = 0;
	let sum_of_money9 = 0;let sum_of_money10 = 0;
	let total_recharge = 0;
	connect((err, db) => {
		if (err) {
			logger.info("数据库链接失败！")
		} else {
			db.collection("userinfo").aggregate([{
					$match: {
						$and: [{
							$or:[{
								server_id: server_id
							}]
						}, {
							total_recharge: {
								"$gt": 0
							}
						}]
					}
				},
				{
					$group: {
						_id: '$total_recharge',
						count: {
							$sum: 1
						}
					}
				},
				{
					$sort: {
						_id: 1
					}
				}
			]).toArray(function(err, result) {
				if (err) {
					logger.info('error:' + err);
					return;
				} else {
				   if(result.length>0){
					result.forEach((item) => {
						if(item._id===0){
							return;
						}else{
							if (0 < item._id && item._id < 1) {
								number_of_people1 += item.count;
								sum_of_money1 += item._id * item.count;
							}
							if (1 <= item._id && item._id < 2) {
								number_of_people2 += item.count;
								sum_of_money2 += item._id * item.count;
							}
							if (2 <= item._id && item._id < 5) {
								number_of_people3 += item.count;
								sum_of_money3 += item._id * item.count;
							}
							if (5 <= item._id && item._id < 10) {
								number_of_people4 += item.count;
								sum_of_money4 += item._id * item.count;
							}
							if (10 <= item._id && item._id < 20) {
								number_of_people5 += item.count;
								sum_of_money5 += item._id * item.count;
							}
							if (20 <= item._id && item._id < 50) {
								number_of_people6 += item.count;
								sum_of_money6 += item._id * item.count;
							}
							if (50 <= item._id && item._id < 100) {
								number_of_people7 += item.count;
								sum_of_money7 += item._id * item.count;
							}
							if (100 <= item._id && item._id < 200) {
								number_of_people8 += item.count;
								sum_of_money8 += item._id * item.count;
							}
							if (200 <= item._id && item._id < 500) {
								number_of_people9 += item.count;
								sum_of_money9 += item._id * item.count;
							}
							if (item._id >= 500) {
								number_of_people10 += item.count;
								sum_of_money10 += item._id * item.count;
							}
						}
					});
					//总人数
					let total_number_of_people = number_of_people1 + number_of_people2 + number_of_people3 + number_of_people4 +
						number_of_people5 +
						number_of_people6 + number_of_people7 + number_of_people8 + number_of_people9 + number_of_people10;
					//人数占比
					let proportion_of_population1 =
						Math.round((number_of_people1 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population2 =
						Math.round((number_of_people2 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population3 =
						Math.round((number_of_people3 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population4 =
						Math.round((number_of_people4 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population5 =
						Math.round((number_of_people5 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population6 =
						Math.round((number_of_people6 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population7 =
						Math.round((number_of_people7 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population8 =
						Math.round((number_of_people8 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population9 =
						Math.round((number_of_people9 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";
					let proportion_of_population10 =
						Math.round((number_of_people10 / total_number_of_people) * 10000) / (100.0).toFixed(2) +
						"%";

					//总金额
					total_recharge = sum_of_money1 + sum_of_money2 + sum_of_money3 + sum_of_money4 + sum_of_money5 +
						sum_of_money6 + sum_of_money7 + sum_of_money8 + sum_of_money9 + sum_of_money10;
					//金额占比
					let amount_proportion1 =
						Math.round((sum_of_money1 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion2 =
						Math.round((sum_of_money2 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion3 =
						Math.round((sum_of_money3 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion4 =
						Math.round((sum_of_money4 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion5 =
						Math.round((sum_of_money5 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion6 =
						Math.round((sum_of_money6 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion7 =
						Math.round((sum_of_money7 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion8 =
						Math.round((sum_of_money8 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion9 =
						Math.round((sum_of_money9 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let amount_proportion10 =
						Math.round((sum_of_money10 / total_recharge) * 10000) / (100.0).toFixed(2) +
						"%";
					let obj1 = {
						section: "[0-0.99]",
						number_of_people: number_of_people1,
						proportion_of_population: proportion_of_population1,
						sum_of_money: sum_of_money1.toFixed(2),
						amount_proportion: amount_proportion1,
					};
					let obj2 = {
						section: "[1-1.99]",
						number_of_people: number_of_people2,
						proportion_of_population: proportion_of_population2,
						sum_of_money: sum_of_money2.toFixed(2),
						amount_proportion: amount_proportion2,
					};
					let obj3 = {
						section: "[2-4.99]",
						number_of_people: number_of_people3,
						proportion_of_population: proportion_of_population3,
						sum_of_money: sum_of_money3.toFixed(2),
						amount_proportion: amount_proportion3,
					};
					let obj4 = {
						section: "[5-9.99]",
						number_of_people: number_of_people4,
						proportion_of_population: proportion_of_population4,
						sum_of_money: sum_of_money4.toFixed(2),
						amount_proportion: amount_proportion4,
					};
					let obj5 = {
						section: "[10-19.99]",
						number_of_people: number_of_people5,
						proportion_of_population: proportion_of_population5,
						sum_of_money: sum_of_money5.toFixed(2),
						amount_proportion: amount_proportion5,
					};
					let obj6 = {
						section: "[20-49.99]",
						number_of_people: number_of_people6,
						proportion_of_population: proportion_of_population6,
						sum_of_money: sum_of_money6.toFixed(2),
						amount_proportion: amount_proportion6,
					};
					let obj7 = {
						section: "[50-99.99]",
						number_of_people: number_of_people7,
						proportion_of_population: proportion_of_population7,
						sum_of_money: sum_of_money7.toFixed(2),
						amount_proportion: amount_proportion7,
					};
					let obj8 = {
						section: "[100-199.99]",
						number_of_people: number_of_people8,
						proportion_of_population: proportion_of_population8,
						sum_of_money: sum_of_money8.toFixed(2),
						amount_proportion: amount_proportion8,
					};
					let obj9 = {
						section: "[200-499.99]",
						number_of_people: number_of_people9,
						proportion_of_population: proportion_of_population9,
						sum_of_money: sum_of_money9.toFixed(2),
						amount_proportion: amount_proportion9,
					};
					let obj10 = {
						section: "[500+]",
						number_of_people: number_of_people10,
						proportion_of_population: proportion_of_population10,
						sum_of_money: sum_of_money10.toFixed(2),
						amount_proportion: amount_proportion10,
					};
					let arr = [];
					arr.push(obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10);
					arr.sort((a, b) => {
						return parseFloat(a.sum_of_money) < parseFloat(b.sum_of_money) ? 1 : -1;
					});
					res.send({
						code: 200,
						result: arr,
						total_recharge: total_recharge,
						msg: '获取成功'
					})
				 }else{
					 res.send({
						 code:205,
						 result:[]
					 })
				 }
			  }
			})
		}
	})
});

//玩家积分查询
router.post('/playerPoints',(req,res)=>{
	let server_id=parseInt(req.body.server_id);
	let start_time=parseInt(req.body.start_time);
	let end_time=parseInt(req.body.end_time);
	connect(function(err,db){
		if(err){
			logger.info("数据库连接错误！",err)
		}else{
			db.collection("active_point").aggregate([
			    {
					$match: {
						$and: [{
							time: {
								"$gte": start_time,
								"$lte": end_time
							}
						}, {
							server_id: server_id
						}]
					}
				},
				{
					$project: {
						_id: 0,
						"uid": 1,
						"vip_level": 1,
						"server_id": 1,
						"data":1,
						"time":1
					}
				},
			]).toArray((err,data)=>{
				if(err){
					return;
				}else{
					var arr=[]
					data.forEach(item=>{
						let data=item.data.split("-")[item.data.split("-").length-1].split(":")[1]
						if(item.data===""){
							return;
						}else{
							let obj={
								uid:item.uid,
								vip_level:item.vip_level,
								server_id:item.server_id,
								data:data,
								time:item.time
							}
							arr.push(obj)
						}
					})
					res.send({
						code:200,
						msg:"success",
						result:arr
					})
				}
			})
		}
	})
})
module.exports = router;
